﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Resources;
using Cesave.Domain.Core;
using System.Data;

namespace Cesave.Domain.PermisosModule.Contracts.Modulos
{
	/// <summary>
	/// 
	/// </summary>
	public class ModuloService
		: IModuloService
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private IModuloRepository _moduloRepository;

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="moduloRepository"></param>
		public ModuloService(IModuloRepository moduloRepository)
		{
			if (moduloRepository == null)
				throw new ArgumentNullException(Messages.exception_DependenciesAreNotInitialized, "moduloRepository");
			_moduloRepository = moduloRepository;
		}

		#endregion

		#region IModuloService Members

		/// <summary>
		/// 
		/// </summary>
		/// <param name="modulo"></param>
		public void AddModulo(Entities.Modulo modulo)
		{
			if (modulo == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "modulo");
			IUnitOfWork unitOfWork = _moduloRepository.StoreContext;
			_moduloRepository.Add(modulo);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="modulo"></param>
		public void ChangeModulo(Entities.Modulo modulo)
		{
			if (modulo == null)
				throw new ArgumentNullException(Messages.exception_InvalidArgument, "modulo");
			IUnitOfWork unitOfWork = _moduloRepository.StoreContext;
			_moduloRepository.Modify(modulo);
			try
			{
				unitOfWork.Commit();
			}
			catch (OptimisticConcurrencyException)
			{
				unitOfWork.RollbackChanges();
				unitOfWork.CommitAndRefreshChanges();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="juntaGuid"></param>
		/// <returns></returns>
		public List<Entities.Modulo> FindModulosByJunta(Guid juntaGuid)
		{
			if (juntaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "juntaGuid");
			ModulosByJuntaSpecification modulosByJuntaSpecification = new ModulosByJuntaSpecification(juntaGuid);
			return _moduloRepository.FindModulosByJunta(modulosByJuntaSpecification).ToList();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="juntaGuid"></param>
		/// <param name="status"></param>
		/// <returns></returns>
		public List<Entities.Modulo> FindModulosByJuntaAndStatus(Guid juntaGuid, bool status = true)
		{
			if (juntaGuid == Guid.Empty)
				throw new ArgumentException(Messages.exception_InvalidArgument, "juntaGuid");
			ModulosByJuntaSpecification modulosByJuntaSpecification = new ModulosByJuntaSpecification(juntaGuid);
			ModulosByStatusSpecification modulosByStatusSpecification = new ModulosByStatusSpecification(status);
			return _moduloRepository.FindModulosByJuntaAndStatus(modulosByJuntaSpecification, modulosByStatusSpecification).ToList();
		}

		#endregion

		#region IDisposable Members

		public void Dispose()
		{
			if (_moduloRepository != null && _moduloRepository.StoreContext != null)
				_moduloRepository.StoreContext.Dispose();
		}

		#endregion
	}
}
